<?php


namespace App\Services\Manager;


use App\Models\Back;
use App\Models\Combo;
use App\Models\Goods;
use App\Models\Sale;
use App\Models\Shop;
use App\Models\UserManager;
use App\Models\UserScoreLog;
use Illuminate\Http\Request;

class SaleService
{
    /**
     * 计算金额
     * @param $saleObject
     * @param $gradeDiscount
     * @return mixed
     */
    public function countPrice($saleObject, $gradeDiscount)
    {
        $totalPrice = 0;
        foreach ($saleObject as &$value) {
            if(!isset($value['isFree'])){
                $value['isFree']  =  0;
            }
            if (!$value['isFree']) {
                $totalPrice = $totalPrice + (($value['salesPrice'] * $value['amount']) * ($value['discount']/100));
            }
        }
        unset($value);
        $data['totalPrice'] = $totalPrice;
        $data['gradeDiscount'] = $gradeDiscount;
        $data['discountPrice'] = intval($totalPrice * ($gradeDiscount / 100));
        return $data;
    }

    /**
     * 积分对换人民币获取需付
     * @return \Illuminate\Http\JsonResponse
     */
    public function useScoreAndRMB($shopId, $payPrice, $useScore)
    {
        $useScoreRule = Shop::where('shopId', $shopId)->value('useScoreRule');
        $scorePrice = intval($useScore / $useScoreRule * 100);
        $data = [];
        $payPrice = $payPrice - $scorePrice;
        return $payPrice;
    }

    /**
     * 计算总和
     * @param $sale
     * @return int|mixed
     */
    public function addPrice($sale,$key)
    {
        $totalPrice = 0;
        foreach ($sale as $value) {
            $totalPrice = $totalPrice + $value[$key];
        }
        return $totalPrice;
    }

    public function addBack($saleId){
        $sale = Sale::select('returnComboId as comboId', 'saleNo', 'returnDay', 'shopId')->where('saleId',
            $saleId)->first();
        if($sale['comboId']){
            $bankList = Back::where('saleNo', $sale['saleNo'])->orderBy('cycle')->get()->toArray();
            if (!isset($bankList[0])) {
                $combo = Combo::where('comboId', $sale['comboId'])->first();
                if(!$combo){
                    return 404;
                }
                $dayArr = [];
                $dayArr[] = $combo['first'];
                $dayArr[] = $combo['second'];
                $dayArr[] = $combo['thirdly'];
                $dayArr[] = $combo['fourth'];
                $addRetrun = [];
                foreach ($dayArr as $key => $value) {
                    if ($value) {
                        $addRetrun[$key]['saleNo'] = $sale['saleNo'];
                        $addRetrun[$key]['shopId'] = $sale['shopId'];
                        $addRetrun[$key]['cycle'] = $key + 1;
                        $addRetrun[$key]['createTime'] = date("Y-m-d H:i:s");
                        $addRetrun[$key]['updateTime'] = date("Y-m-d H:i:s");
                        $addRetrun[$key]['returnDayNum'] = $value;
                        $addRetrun[$key]['returnDay'] = date("Y-m-d", strtotime('+ ' . $value . ' DAY'));
                        if ($key == 0) {
                            $addRetrun[$key]['isStart'] = 1;
                        } else {
                            $addRetrun[$key]['isStart'] = 0;
                        }
                    } else {
                        break;
                    }
                }
                Back::insert($addRetrun);
            } else {
                $bankIdList = [];
                $sql = 'update back set returnDay = case ';
                foreach ($bankList as $key => $value) {
                    $bankIdList[] = $value['backId'];
                    $sql = $sql . " when backId = " . $value['backId'] . " then '" . date('Y-m-d',
                            strtotime('+ ' . $value['returnDayNum'] . ' DAY')) . "'";
                }

                $sql = $sql . " end , isStart = case when backId = " . $bankList[0]['backId'] . " then 1 else 0 end , isShow = 0 , backStatus = 0 ,updateTime = '" . date("Y-m-d H:i:s") . "' where backId in (" . implode(',',
                        $bankIdList) . ")";

                //return var_dump($sql);
                \DB::update(\DB::raw($sql));
            }
        }
        return 1;
    }

    public function addUserScoreLog($data,$userId,$managerId,$shopId)
    {
        $data['createTime']  = date("Y-m-d H:i:s");
        if($data['scoreNum'] != 0){
            $data['userId'] = $userId;
            $data['managerName'] = UserManager::where('managerId',$managerId)->value('name');
            $data['shopId'] = $shopId;
            $data['shopName'] = Shop::where('shopId',$shopId)->value('shopName');
            if($data['scoreNum'] < 0){
                $data['logType']  = "OUT";
                $data['scoreNum'] = abs($data['scoreNum']);
            }else{
                $data['logType']  = "ENTER";
            }
            UserScoreLog::insert($data);
        }
        return $data;
    }
}